package jdbc;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
//PreparedStatement的增删改
public class jdbc_test3 {
    public static void main(String[] args) {
        Connection conn = null;
        //Statement stmt = null;
        PreparedStatement ps = null;
        //jdbc:mysql://localhost:3306/test1
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/my_user", "root", "314159");
            String sql = "insert into test01 (name, password)  values (?,?)";
            //问号表示占位符  进行欲编译
            ps = conn.prepareStatement(sql);
            //填充占位符
            ps.setString(1, "jack");
            ps.setString(2, "123");
            int i = ps.executeUpdate();
            System.out.println(i);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
    }
}
